package ee.difference.beans;

import ee.difference.principal.Difference;
import ee.difference.principal.InvalidEntryException;

public class PropertyDifference extends Difference<PropertyDescriptorEntry> {

	String name;
	
	public PropertyDifference(PropertyDescriptorEntry _leftEntry, PropertyDescriptorEntry _rightEntry) {

		super(_leftEntry, _rightEntry);
		
		if(_rightEntry == null && _leftEntry == null){
			throw new InvalidEntryException();
		}
		
		if(_rightEntry == null || _leftEntry == null){
			this.setDefference(true);
		}
		
		if(_rightEntry!= null && _leftEntry != null && (!_rightEntry.getKey().equals(_leftEntry.getKey()))){
			throw new InvalidEntryException();
		}
		
		if(_leftEntry != null)
			this.name = _leftEntry.getKey();
		else
			this.name = _rightEntry.getKey();
		
		if((!this.isDefference()) && isNull(_leftEntry.getEntry().getReadMethod()) == isNull(_rightEntry.getEntry().getReadMethod())
				&& isNull(_leftEntry.getEntry().getWriteMethod()) ==isNull(_rightEntry.getEntry().getWriteMethod())){
			
			this.setDefference(false);
			
		}
	}
	
	boolean isNull(Object o){
		return (o==null) ? true: false;
	}
	
	public String toStringDescription(){
		String desc = "PROPERTY NAME:" + this.name + "\t\t" 
					+ (this.isDefference() ?"[X]":"[O]") + " "  
					+ "Left: " + getEntryDesc(this.getLeftEntry())
					+ "Right:" + getEntryDesc(this.getRightEntry());
		
		return desc;
	}
	
	private String getEntryDesc(PropertyDescriptorEntry entry){
		
		if(entry == null)
			return "[set -][get -]";
		
		String desc = "";
		if(entry.getEntry().getWriteMethod() != null){
			desc += "[set o]";
		}else{
			desc += "[set x]";
		}
		
		if(entry.getEntry().getReadMethod() != null){
			desc += "[get o]";
		}else{
			desc += "[get x]";
		}

		return desc;
		
	}
	

}
